package com.cpkso.yzx.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import runze.util.security.MD5Util;

/**
 * @author wrzhxy@qq.com
 * @date 2017年11月10日
 */
@Entity
@Table(name="t_user")
public class User {
	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	@Column(nullable=false)
	private String name;
	@Column(nullable=false)
	private String password;
	@ManyToMany
	@JoinTable(name="t_user_role", joinColumns=@JoinColumn(name="uid")
		, inverseJoinColumns=@JoinColumn(name="rid"))
	private Set<Role> roles = new HashSet<>();
	
	public User() {
	}
	public User(Integer id) {
		this.id = id;
	}

	/**
	 * @param name		姓名
	 * @param password	密码
	 */
	public User(String name, String password) {
		this.name = name;
		this.password = "".equals(password) ? null : MD5Util.md5(password);
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = "".equals(name) ? null : name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = "".equals(password) ? null : MD5Util.md5(password);
	}
	public Set<Role> getRoles() {
		return roles;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	
}
